package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdWmChannelDto;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.BagUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Override
    public ResponseResult listAll() {
        return ResponseResult.okResult(this.list());
    }

    @Override
    public ResponseResult listChannel(AdWmChannelDto dto) {
        //校验参数，搜索字符为空时查询全部
        dto.checkParam();
        IPage page = new Page(dto.getPage(), dto.getSize());
        if (StringUtils.isEmpty(dto.getName())) {
            List<WmChannel> list = list();//获取到所有频道信息
            LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = Wrappers.<WmChannel>lambdaQuery().orderByDesc(WmChannel::getCreatedTime);
            page.setRecords(list);
            IPage iPage = page(page, lambdaQueryWrapper);
            return ResponseResult.okResult(iPage.getRecords());
        }
        //模糊查询
        IPage iPage = page(page, Wrappers.<WmChannel>lambdaQuery().like(WmChannel::getName, dto.getName()).orderByDesc(WmChannel::getCreatedTime));
        return ResponseResult.okResult(iPage.getRecords());
    }

    @Override
    public ResponseResult saveChannel(WmChannelDto wmChannel) {
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        //频道如果已存在或者参数为空返回参数错误
        if (StringUtils.isEmpty(wmChannel.getName()) || channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel channel1 = new WmChannel();
        BeanUtils.copyProperties(wmChannel, channel1);
        channel1.setCreatedTime(new Date());
        save(channel1);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateChannel(WmChannelDto channel) {
        if (channel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmChannel wmChannel = getById(channel.getId());
        BeanUtils.copyProperties(channel, wmChannel);
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delChannel(Integer id) {
        WmChannel channel = getById(id);
        if (channel.isStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        } else {
            boolean flag = removeById(id);
            if (flag) {
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}